package com.demo.modalframe3;

import com.demo.modalframe3.listener.CloseWindowListener;
import com.demo.modalframe3.utils.GraphicsUtil;
import com.demo.modalframe3.utils.UIUtil;
import com.esotericsoftware.tablelayout.swing.Table;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

public class Frame3 extends JFrame {

    private JLabel operateLabel;

    public Frame3() {
        initFrame();

        this.setTitle("Frame 3(application exclude)");
        this.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
        this.setMinimumSize(new Dimension(GraphicsUtil.Frame_Width, GraphicsUtil.Frame_Height));
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        this.addWindowListener(new CloseWindowListener());
    }

    private void initFrame() {
        JButton button1 = new JButton("Button 1");
        button1.addActionListener(e -> {
            handleButtonClick(e);
        });
        JButton button2 = new JButton("Button 2");
        button2.addActionListener(e -> {
            handleButtonClick(e);
        });
        JButton button3 = new JButton("Button 3");
        button3.addActionListener(e -> {
            handleButtonClick(e);
        });

        JLabel label = new JLabel("当前操作： ");
        operateLabel = new JLabel("");
        operateLabel.setForeground(Color.red);

        Table contentTable = UIUtil.defaultTable();
        contentTable.addCell(button1).colspan(2).row();
        contentTable.addCell(button2).colspan(2).row();
        contentTable.addCell(button3).colspan(2).row();
        contentTable.addCell(label);
        contentTable.addCell(operateLabel);

        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(contentTable, BorderLayout.CENTER);
    }

    private void handleButtonClick(ActionEvent event) {
        operateLabel.setText(event.getActionCommand() + " clicked.");
    }
}
